//
//  AppDelegate.m
//  ToWhere
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "AppDelegate.h"
#import "TravelViewController.h"
#import "MusicViewController.h"
#import "MenuViewController.h"
#import "EntertainmentViewController.h"
#import "MyViewController.h"
#import "YRSideViewController.h"
#import "SaveTools.h"
#import "UMSocial.h"
#import "GuideViewController.h"

@interface AppDelegate ()<MenuViewControllerDelegate, GuidePageDelegate>

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_yrsideView release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.backgroundColor = [UIColor whiteColor];
     [self preferredStatusBarStyle];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    [UMSocialData setAppKey:@"5655ae58e0f55a464d000a1c"];  //分享

    NSString *string = [SaveTools readFromFileName:@"guidePage" andFilePath:NSLibraryDirectory];
    if (string == nil) {
#pragma mark 引导页
        
        GuideViewController *guide = [[GuideViewController alloc]init];
        guide.delegate = self;
        self.window.rootViewController = guide;
        [guide release];
//        [SaveTools saveToFileName:@"guidePage" andFilePath:NSLibraryDirectory andVaule:@"0"];
    }
    else
    {
#pragma mark 创建TabBar
        [self createTabBar];
    }

    
    return YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark 引导页的代理方法
-(void)enterHomepage
{
#pragma mark 创建TabBar
    [self createTabBar];
}


//创建TabBar
-(void)createTabBar
{
    TravelViewController *travel = [[TravelViewController alloc]init];
    UINavigationController *travelNavigation = [[UINavigationController alloc]initWithRootViewController:travel];
    travelNavigation.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"旅行" image:[[UIImage imageNamed:@"TabBar_Travel"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"TabBar_Travel_select"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    MusicViewController *music = [[MusicViewController alloc]init];
    UINavigationController *musicNavigation = [[UINavigationController alloc]initWithRootViewController:music];
    musicNavigation.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"音旅" image:[[UIImage imageNamed:@"TabBar_Music"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"TabBar_Music_select"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    EntertainmentViewController *entertainment = [[EntertainmentViewController alloc]init];
    UINavigationController *enterNavigation = [[UINavigationController alloc]initWithRootViewController:entertainment];
    enterNavigation.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"游记" image:[[UIImage imageNamed:@"TabBar_Entain"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"TabBar_Entain_select"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    MyViewController *my = [[MyViewController alloc]init];
    UINavigationController *myNavigation = [[UINavigationController alloc]initWithRootViewController:my];
    myNavigation.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[[UIImage imageNamed:@"TabBar_My"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"TabBar_My_select"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    UITabBarController *tabBar = [[UITabBarController alloc]init];
    tabBar.viewControllers = @[travelNavigation, enterNavigation, musicNavigation,myNavigation];
    tabBar.tabBar.barTintColor = [UIColor blackColor];
    tabBar.tabBar.tintColor = [UIColor orangeColor];
    
    
    MenuViewController *left = [[MenuViewController alloc] init];
    left.delegate = self;
    self.yrsideView = [[YRSideViewController alloc] initWithNibName:nil bundle:nil];
    self.yrsideView.rootViewController = tabBar;
    self.yrsideView.leftViewController = left;
    
    self.window.rootViewController = self.yrsideView;

    [left release];
    [travel release];
    [travelNavigation release];
    [musicNavigation release];
    [music release];
    [enterNavigation release];
    [entertainment release];
    [my release];
    [myNavigation release];
    [tabBar release];
    
#pragma mark 消息中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"抽屉关" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationOne:) name:@"抽屉开" object:nil];
    
    NSString *day = [SaveTools readFromFileName:@"sun" andFilePath:NSLibraryDirectory];
    if (day == nil) {
        [SaveTools saveToFileName:@"sun" andFilePath:NSLibraryDirectory andVaule:@"0"];
    }
    if ([day isEqualToString:@"1"]) {
        [self.window setBackgroundColor:[UIColor blackColor]];
        [self.window setAlpha:0.5];
    }
    else{
        [self.window setBackgroundColor:[UIColor blackColor]];
        [self.window setAlpha:1];
    }

}

-(void)receiveNotification:(NSNotification *)notification
{
    self.yrsideView.needSwipeShowMenu = NO;
}
-(void)receiveNotificationOne:(NSNotification *)notification
{
    self.yrsideView.needSwipeShowMenu = YES;
}

#pragma mark 我的页面的代理方法
-(void)sendApp
{
    [self.yrsideView hideSideViewController:true];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
